package org.jeecg.modules.abs;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.tencent.response.IMBaseResponse;
import org.jeecg.modules.config.CustomProperties;
import org.jeecg.modules.tencent.enums.IMCallbackEnum;

import javax.annotation.Resource;

/**
 * @author guoyh
 * @ClassName CententCallBackAbstract
 * @Description 腾讯IM回调抽象类
 * @since 2020/8/28 23:25
 **/
public abstract class TencentCallBackAbstract {

    @Resource
    private CustomProperties customProperties;

    /**
     * 获取处理实现类
     * @return
     */
    public abstract IMCallbackEnum getHandleClass();

    /**
     * 回调
     * @param sdkAppId
     * @param callBackCommand
     * @param contentType
     * @param clientIP
     * @param optPlatform
     * @param body
     * @return
     */
    public IMBaseResponse callBack(String sdkAppId,String callBackCommand,String contentType,
                                   String clientIP,String optPlatform,String body){
        //未通过校验，返回错误
        if (!check(sdkAppId, callBackCommand, contentType, clientIP, optPlatform, body)){
            return IMBaseResponse.buildFail("基础校验未通过");
        }
        return handle(body);
    }

    private boolean check(String sdkAppId,String callBackCommand,String contentType,
                          String clientIP,String optPlatform,String body){
        boolean hasEmpty = StringUtils.isBlank(sdkAppId) ||
                StringUtils.isBlank(callBackCommand) ||
                StringUtils.isBlank(contentType) ||
                StringUtils.isBlank(clientIP) ||
                StringUtils.isBlank(optPlatform) ||
                StringUtils.isBlank(body);
        return hasEmpty ? false : StringUtils.equals(sdkAppId,customProperties.getIm().getAppid().toString());
    }

    /**
     * 处理
     * @param body
     * @return
     */
    protected abstract IMBaseResponse handle(String body);

}
